//
//  ForumView.m
//  cityCar
//
//  Created by yao on 14-11-3.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ForumView.h"
#import "Connection.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "Forum.h"
@implementation ForumView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.bigArray = [NSMutableArray array];
        self.countx = 1;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setupRefresh];
}
- (void)netWrok
{
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/bbs/hot?pageNo=%d&pageSize=20", self.countx];
    //    NSString *str = @"http://mrobot.pcauto.com.cn/v3/bbs/hot?pageNo=1&pageSize=20";
    
    [Connection networkHandlerWith:str block:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"topicList"];

        for (NSDictionary *dic in array) {
            
            Forum *forum = [[Forum alloc] init];
            [forum setValuesForKeysWithDictionary:dic];
            forum.url = [NSString stringWithFormat:@"http://bbs.pcauto.com.cn/topic-%@.html", forum.topicId];
            [self.bigArray addObject:forum];
            [forum release];
        }
        
        [self.collectionView reloadData];
        
    }];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.collectionView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.collectionView footerEndRefreshing];
        self.countx += 1;
        NSLog(@"self.countx %d", self.countx);
    });
    
    
    
}
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.collectionView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.collectionView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.collectionView addFooterWithTarget:self action:@selector(netWrok)];
    [self.collectionView footerEndRefreshing];
    
//    self.countx = 2;
//    NSLog(@"se.countx %d", self.countx);
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 刷新表格
        [self.collectionView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.collectionView  headerEndRefreshing];
    });
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 获得ID
    Forum *forum = [self.bigArray objectAtIndex:indexPath.item];
    
    self.block(forum);
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
